function save_info(p_fuseaction){
	if (verify(document.forms(0))){
		document.forms(0).fuseaction.value = p_fuseaction;
	} else {
		return;
	}
	document.forms(0).hdn_fisrt.value = eval(document.forms(0).hdn_fisrt.value) + 1;
	btn_save_onclick(p_fuseaction);
}
// begin 01/10/2008 khi click nut cap nhap thi nut cap nhap bi disable
//function click_disable(obj){
	//obj.disabled=true;	
//}
//end
function rad_onclick(rad_obj,hdn_obj){
	hdn_obj.value = rad_obj.value;
}
function Update_record_status(p_fuseaction){
	if (document.forms[0].rad_status.value == '')
		alert('Phải xác định kết quả thụ lý.');
	else{
		document.forms(0).fuseaction.value = p_fuseaction;
		document.forms(0).submit();
	}
}
function btn_query_onclick(){
	v_current_url = "index.php";
	v_record_id = document.forms(0).hdn_record_id.value;
	v_fuseaction = document.forms(0).fuseaction.value;
	v_recordtype_filter = document.forms(0).hdn_recordtype_filter.value;
	v_current_url = v_current_url + "?fuseaction=" + v_fuseaction + "&hdn_record_id=" + v_record_id + "&hdn_recordtype_filter=" + v_recordtype_filter + "&hdn_query_data=1"
	//alert (v_current_url); 
	window.location = v_current_url;
}
function btn_approve_leader_onclick(p_checkbox_obj, p_hidden_obj, p_fuseaction){
	if (!checkbox_value_to_list(p_checkbox_obj,",")){
		alert("Phải xác định hồ sơ.");
	}	
	else{
		document.forms(0).fuseaction.value = p_fuseaction;
		p_hidden_obj.value = checkbox_value_to_list(p_checkbox_obj,",");
		var x =  document.f_dsp_all_record_approved.sel_recordtype;
		document.f_dsp_all_record_approved.value = x.options[x.selectedIndex].text
		document.forms(0).submit(); 
	}	
}
function btn_handling_record_onclick(p_checkbox_obj, p_hidden_obj, p_fuseaction){
	if (!checkbox_value_to_list(p_checkbox_obj,",")){
		alert("Phải xác định hồ sơ.");
	}	
	else{
		document.forms(0).fuseaction.value = p_fuseaction;
		p_hidden_obj.value = checkbox_value_to_list(p_checkbox_obj,",");
		var x =  document.f_dsp_all_handling_record_.sel_recordtype;
		document.f_dsp_all_handling_record_.value = x.options[x.selectedIndex].text
		document.forms(0).submit(); 
	}	
}
function show_dialog_all_item_onclick(p_goto_url,p_fuseaction, p_text_name_obj, p_text_code_obj, p_hdn_obj, p_hdn_unit_id,p_hdn_handler_id_list){
	if (p_hdn_unit_id == ''){
	 alert ('Phải xác định phòng thụ lý hồ sơ.'); return;
	}
	//alert(p_hdn_unit_id);
	v_url =  p_goto_url + "?hdn_handle_agency_id="+p_hdn_unit_id +"&hdn_handler_id_list="+p_hdn_handler_id_list + "&fuseaction=" + p_fuseaction + "&modal_dialog_mode=1" + "&" + randomizeNumber();
	sRtn = showModalDialog(v_url,"","dialogWidth=420pt;dialogHeight=280pt;dialogTop=80pt;status=no;scroll=no;");
	if (!sRtn) return;
	arr_value = sRtn.split(_LIST_DELIMITOR);
	p_hdn_obj.value = arr_value[0];
	p_text_code_obj.value = arr_value[1];		
	p_text_name_obj.value = arr_value[2];
}
//---------------------------
function set_value_hidden(obj,obj_value){
	obj.value = obj_value;
}
//nieuvv--181008-sort du lieu dung jQuery


var c_id = new Array();						// stt
var c_code = new Array();				// ma ho so
var register_name = new Array();		// ten nguoi dai dien
var c_address = new Array();			// dia chi
var c_received_date = new Array();		// ngay tiep nhan
var c_appointed_date = new Array();		// ngay hen tra
var c_current_status = new Array();		// trang thai
var c_cmnd = new Array();				// so cmnd
var c_handle = new Array();				// can bo thu ly
var hiddens = new Array();

// luu du lieu 
// num_record: tong so record 
// num_col : tong so cot
function saveData(num_record,num_col)
{		
	var hidden = document.getElementById('hdn_id');
	hiddens[0] = hidden.value;
	
	hidden = document.getElementById('hdn_code');
	hiddens[1] = hidden.value;
	
	hidden = document.getElementById('hdn_name');
	hiddens[2] = hidden.value;
	
	hidden = document.getElementById('hdn_address');
	hiddens[3] = hidden.value;
	
	hidden = document.getElementById('hdn_received');
	hiddens[4] = hidden.value;

	hidden = document.getElementById('hdn_cmnd');
	hiddens[5] = hidden.value;
	//Lay du lieu hien co tren table
	var index ;
	for( index = 0 ; index < num_record ; index++)
	{		
		var x = document.getElementById(index*num_col + 0);//id cua cac td!
		c_id[index] = x.innerHTML;		
		//alert(c_id[index]);
		
		x = document.getElementById(index*num_col + 1);//id cua cac td!
		c_code[index] = x.innerHTML;
		//alert(c_code[index]);
		
		x = document.getElementById(index*num_col + 2 );
		register_name[index] = x.innerHTML;	
		//alert(register_name[index]);
		
		x = document.getElementById(index*num_col + 3 );
		c_address[index] = x.innerHTML;		
		//alert(c_address[index]);
		
		x = document.getElementById(index*num_col + 4 );
		c_received_date[index] = x.innerHTML;
		//alert(c_received_date[index]);
		if(num_col>5)
		{
			x = document.getElementById(index*num_col + 5 );
			c_cmnd[index] = x.innerHTML;
			//alert(c_cmnd[index]);
		}		
	}		
}
//hien thi danh sach ho so sau khi sap xep
function ShowData(num_record,num_col)
{
	var hidden = document.getElementById('hdn_id');
	hidden.value = hiddens[0];
	
	hidden = document.getElementById('hdn_code');
	hidden.value = hiddens[1];
	
	hidden = document.getElementById('hdn_name');
	hidden.value = hiddens[2];
	
	hidden = document.getElementById('hdn_address');
	hidden.value = hiddens[3];
	
	hidden = document.getElementById('hdn_received');
	hidden.value = hiddens[4];

	hidden = document.getElementById('hdn_cmnd');
	hidden.value = hiddens[5];
	var ind ;
	for( ind = 0 ; ind < num_record ; ind++)
	{	
		var x = document.getElementById(ind*num_col + 0);//id cua cac td!
		x.innerHTML = c_id[ind];
		
		x = document.getElementById(ind*num_col + 1);//id cua cac td!
		x.innerHTML =c_code[ind] ; 
		
		x = document.getElementById(ind*num_col + 2 );
		x.innerHTML = register_name[ind];
		
		x = document.getElementById(ind*num_col + 3 );
		x.innerHTML = c_address[ind];
		
		x = document.getElementById(ind*num_col + 4 );
		x.innerHTML = c_received_date[ind];
		if(num_col > 5){
			x = document.getElementById(ind*num_col + 5 );
			x.innerHTML = c_cmnd[ind];
		}
	//	x = document.getElementById(i*10 + 5 );
	//	c_appointed_date[i] = x.innerHTML;
	}
}
// hoan doi dong
function ExchangeRow(i,j)
{
	
	var temp_arrang;
	
	temp_arrange = c_id[i];
	c_id[i] = c_id[j];
	c_id[j] = temp_arrange;
	// hoan vi ma ho so
	temp_arrange = c_code[i];
	c_code[i] = c_code[j];
	c_code[j] = temp_arrange;
	// hoan vi ten nguoi dai dien
	temp_arrange = register_name[i];
	register_name[i] = register_name[j];
	register_name[j] = temp_arrange;
	// hoan vi dia chi
	temp_arrange = c_address[i];
	c_address[i] = c_address[j];
	c_address[j] = temp_arrange;
	// hoan vi ngay hen tra
	temp_arrange = c_appointed_date[i];
	c_appointed_date[i] = c_appointed_date[j];
	c_appointed_date[j] = temp_arrange;
	
	temp_arrange = c_received_date[i];
	c_received_date[i] = c_received_date[j];
	c_received_date[j] = temp_arrange;
	
	temp_arrange = c_cmnd[i];
	c_cmnd[i] = c_cmnd[j];
	c_cmnd[j] = temp_arrange;
}

// so sanh theo ten nguoi dai dien
// tra ve 1 : name1 > name2
// tra ve 0 : name1 = name2
// tra ve -1 : name1 < name2
function CompareName(name1,name2)
{	
	var ht1 = new Array();
	ht1 = name1.split(" ");
	var ht2 = new Array();
	ht2 = name2.split(" ");
	var i,j;
	var len1 = ht1.length;
    var len2 = ht2.length;

	//dau tien so sang ten truoc 
	if( ht1[len1-1] > ht2[len2-1])//du 1 khoang trang cuoi cung cua hoten ?
		return 1;
	else if( ht1[len1 -1] == ht2[len2-1])
	{            
		for(i = 0 ; i < len1 ; i ++)
		{
			if(ht1[i] > ht2[i])
				return 1;
			else if(ht1[i] == ht2[i])
				return 0;
			else
				return -1;
		}
	}     
	else if( ht1[len1-1] < ht2[len2-1])
		return -1;	
}
// so sanh so cmnd
// tra ve 1: cmnd1 > cmnd2 
// tra ve 0: cmnd1 = cmnd2
// tra ve -1: cmnd1 < cmnd1
function CompareCMND(cmnd1,cmnd2)
{
	if(cmnd1 > cmnd2)
		return 1;
	else if(cmnd1 == cmnd2)
		return 0;
	else
		return -1;
}
//so sanh theo ma ho so
// tra ve 1 : code1 > code2
// tra ve 0 : code1 = code2
// tra ve -1: code1 < code2
function CompareCode(code1,code2)
{
	var c1 = new Array();
	c1 = code1.split(".");
	var c2 = new Array();
	c2 = code2.split(".");
	var len1 = c1.length;
	var len2 = c2.length;
	var i,j;
	if(c1[len1-2] > c2[len2-2]) 	// du khoang trang cuoi cung
		return 1;
	else if(c1[len1-1] == c2[len2-1])
	{
		for(i = 0 ; i < len1; i++ )
		{
			if(c1[i] > c2[i])
				return 1;
			else if(c1[i] == c2[i])
				return 0;
			else
				return -1;
		}
	}
	else 
		return -1;
}
// so sanh theo ngay 
// tra ve 1: date1 > date2
// tra ve 0: date1 = date2
// tra ve -1: date1< date2
function CompareDate(date1,date2)
{
//	alert(date1);
	//alert(date2);
	var dt1 = new Array();
	var dt2 = new Array();
	dt1 = date1.split("/");		// tach ra bang dau '/'
	dt2 = date2.split("/");
	var nam1 , nam2,thang1,thang2,ngay1,ngay2;
	nam1 = Math.abs(dt1[2]);//chuyen tu kieu chuoi sang so de so sanh!
	nam2 = Math.abs(dt2[2]);
	thang1 = Math.abs(dt1[1]);
	thang2 = Math.abs(dt2[1]);
	ngay1 =  Math.abs(dt1[0]);
	ngay2 = Math.abs(dt2[0]);
	
	if(nam1 > nam2)
		return 1;
	else if( nam1 == nam2)
	{
		if(thang1 > thang2)//javascrip so sanh bi sai ngay day!
			return 1;
		else if( thang1 == thang2)
		{
			if( ngay1 > ngay2 )
				return  1;
			else if ( ngay1 == ngay2)
				return 0;
			else 
				return -1;        
		}    
		else 
			return -1;        
	}
	else 
		return -1;
}
// sap xep theo ma ho so
function SortByCode(num_record,num_col)
{	
	saveData(num_record,num_col);		// luu du lieu		
	if(hiddens[1] == 1)			// hidden cua ma ho so
	{
		hiddens[1] = 0;		
		var i ,j ;
		for(i  = 0 ; i < num_record-1 ; i++)
		  for( j = i+ 1 ; j < num_record ; j++)
		  {			
			  if( CompareCode(c_code[j],c_code[i]) <= 0)
			  {
				ExchangeRow(i,j);			 
			  }
		  }		
	}
	else			// nguoc lai hidden = 0 :sap giam
	{
		hiddens[1] = 1;	
		var i ,j ;
		for(i  = 0 ; i < num_record-1 ; i++)
		  for( j = i+ 1 ; j < num_record ; j++)
		  {			
			  if( CompareCode(c_code[j],c_code[i]) > 0)
			  {
				ExchangeRow(i,j);			 
			  }
		  }		
	}	
	ShowData(num_record,num_col);
}
// sap xep theo ten nguoi dai dien
function SortByName(num_record,num_col)
{	
	saveData(num_record,num_col);		// luu du lieu
	if(hiddens[2] == 1)			// hidden cua ma ho so: = 1: sap tang
	{
		hiddens[2] = 0;
		var i ,j ;
		for(i  = 0 ; i < num_record-1 ; i++)
		  for( j = i+ 1 ; j < num_record ; j++)
		  {			
			  if( CompareName(register_name[j],register_name[i]) <= 0)
			  {
					ExchangeRow(i,j);			 
			  }
		  }		
	}
	else			// nguoc lai hidden = 0 :sap giam
	{
		hiddens[2] = 1;	
		var i ,j ;
		for(i  = 0 ; i < num_record-1 ; i++)
		  for( j = i+ 1 ; j < num_record ; j++)
		  {
			  if( CompareName(register_name[j],register_name[i]) > 0)
			  {
				ExchangeRow(i,j);			 
			  }
		  }		
	}	
	ShowData(num_record,num_col);
}
// sap xep theo ngay thang
function SortByDate(num_record,num_col)
{
	saveData(num_record,num_col);		// luu du lieu
	if(hiddens[4] == 1)			// hidden cua ma ho so: = 1: sap tang
	{
		hiddens[4] = 0;
		var i ,j ;
		for(i  = 0 ; i < num_record-1 ; i++)
		  for( j = i+ 1 ; j < num_record ; j++)
		  {			
			  if(CompareDate(c_received_date[j],c_received_date[i]) <= 0)
			  {
				ExchangeRow(i,j);			 
			  }
		  }		
	}
	else			// nguoc lai hidden = 0 :sap giam
	{
		hiddens[4] = 1;	
		var i ,j ;
		for(i  = 0 ; i < num_record-1 ; i++)
		  for( j = i+ 1 ; j < num_record ; j++)
		  {			
			  if(CompareDate(c_received_date[j],c_received_date[i]) > 0)
			  {
				ExchangeRow(i,j);			 
			  }
		  }		
	}	
	ShowData(num_record,num_col);
}
// sap xep theo cmnd
function SortByCMND(num_record,num_col)
{
	saveData(num_record,num_col);
	if(hiddens[5] == 1)
	{
		hiddens[5] = 0;
		var i ,j ;
		for(i  = 0 ; i < num_record-1 ; i++)
		  for( j = i+ 1 ; j < num_record ; j++)
		  {			
			  if(CompareCMND(c_received_date[j],c_received_date[i]) <= 0)
			  {
				ExchangeRow(i,j);			 
			  }
		  }		
	}
	else
	{
		hiddens[5] = 1;	
		var i ,j ;
		for(i  = 0 ; i < num_record-1 ; i++)
		  for( j = i+ 1 ; j < num_record ; j++)
		  {			
			  if(CompareCMND(c_received_date[j],c_received_date[i]) > 0)
			  {
				ExchangeRow(i,j);			 
			  }
		  }		
	}
	ShowData(num_record,num_col);
}
// sap xep tong quat
function Arange(p_type,num_record,num_col)
{
	//alert(p_type);
	switch(p_type)
	{
		case 1:
			//alert("1");
			SortByCode(num_record,num_col);
			break;
		case 2:
			//alert("2");
			SortByName(num_record,num_col);
			break;
		case 3:
			//alert("3");			
			SortByName(num_record,num_col);
			break;
		case 4:
			//alert("4");
			SortByDate(num_record,num_col);
			break;
		case 5:
			//alert("5");			
			SortByCMND(num_record,num_col);
			break;
		default:
			//alert("6");
			SortByName(num_record,num_col);
			break;
	}		
}